import basicModule, { IBaseState } from '@/store/baseModule'

import { Notification, Message, Loading } from 'element-ui'

import { get, defaultsDeep, cloneDeep } from 'lodash'

import { Commit, Dispatch } from 'vuex';
import dayjs from 'dayjs'

// TODO 最后一行的接口是在重构中准备弃用的
import {
    addNotice, getNoticeList, getNoticeDetail, deleteNotice, getTotal, editNotice, cancelChange, commitvisible,
    commitNotice, approvalNotice, addProduct, editProduct, deleteProduct
} from '@/api/notice'



export interface State {

}

const state: State = {
    total: {},
    totalNotice: 0,
    noticeSearch: {
        beginTime: '', //查询开始日期
        endTime: '', //查询结束日期
        orderUnit: '', //订货单位
        workingNumber: '', //产品工号,
        state: '',
        page: 1,
        pageSize: 20,
        visibility: false
    },
    noticeList: [],
    notice: {
        noticeId: '',
        orderUnit: '',
        noticeNumber: '',
        workingNumber: '',
        state: '未提交',
        deliveryState: '',
        deliveryPlace: '',
        deliveryMethod: '',
        inBatches: true, // 默认为统一
        deliveryDate: '',
        remark: ''
    },
    productList: [],
    handleList: [],

};

const getters = {};


const mutations = {

    SET_TOTAL(state: any, payload: any) {
        state.total = payload
    },
    SET_TOTALNOTICE(state: any, payload: any) {
        state.totalNotice = payload
    },
    SET_NOTICE_SEARCH(state: any, payload: any) {
        state.noticeSearch = payload
    },
    SET_NOTICE_LIST(state: any, payload: any) {
        state.noticeList = payload
    },
    SET_NOTICE(state: any, payload: any) {
        state.notice = payload
    },
    SET_PRODUCT_LIST(state: any, payload: any) {
        state.productList = payload
    },
    SET_HANDLE_LIST(state: any, payload: any) {
        state.handleList = payload
    },

};

const actions = {

    // 更新生产通知页面数据
    COMMIT_TABLE_DATA({ commit, state, rootState }: any) {

        const positionName = rootState.Login.positionName

        let params = state.noticeSearch
        let t = null

        getNoticeList(params).then((resp) => {
            let noticeList = get(resp, 'data', [])
            noticeList = positionName === '系统管理员' ? noticeList : noticeList.filter(item => { return item.visibility === true })
            commit('SET_NOTICE_LIST', noticeList)
        })

        getTotal(params).then((res) => {
            let totaldata = get(res, 'data', {})
            commit('SET_TOTAL', totaldata)
        })
    },


    // 02_01_新建生产通知
    async ADD_NOTICE({ state, commit, rootState, dispatch }: any, payload: any) {
        let resp = await addNotice(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            // Message.success('新建成功!');
            let data = get(resp, 'data', []);
            return { noticeId: data.noticeId, noticeNumber: data.noticeNumber }
        }
    },

    // 02_02_查询生产通知
    async GET_NOTICE({ state, commit, rootState, dispatch }: any, payload: any) {
        // const positionName = rootState.Login.positionName
        commit('SET_NOTICE_SEARCH', payload)

        let resp = await getNoticeList(payload).catch((err: any) => {
            Notification.error(err)
        })
        let res = await getTotal(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp && res) {
            let totaldata = get(res, 'data', [])
            let noticeList = get(resp, 'data', [])
            let noticeNum = get(resp, 'total', 0)
            // noticeList = positionName === '系统管理员' ? noticeList : noticeList.filter(item => { return item.visibility === true })
            commit('SET_TOTAL', totaldata)
            commit('SET_NOTICE_LIST', noticeList)
            commit('SET_TOTALNOTICE', noticeNum)
            return true
        }
    },

    // 02_03_查询通知单详情
    async GET_NOTICE_DETAIL({ state, commit, rootState, dispatch }: any, payload: any) {
        let resp = await getNoticeDetail(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            let data = get(resp, 'data', []);
            commit('SET_PRODUCT_LIST', data.productList);
            commit('SET_HANDLE_LIST', data.handleList);
            return true;
        }

    },

    // 02_04_删除生产通知
    async DETELE_NOTICE({ state, commit, rootState, dispatch }: any, payload: any) {

        let resp = await deleteNotice(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            Message.success('删除成功!')
            dispatch('COMMIT_TABLE_DATA')
            return true
        }
    },



    // 02_06_修改生产通知
    async EDIT_NOTICE({ state, commit, rootState, dispatch }: any, payload: any) {
      let flag = true;
        let resp = await editNotice(payload).catch((err: any) => {
            Notification.error(err);
            flag = false;
        })

        if (resp && flag) {
            Message.success('保存成功!');
            // dispatch('COMMIT_TABLE_DATA');
            return true;
        }
        return false;
    },

    // TODO 02_07_取消变更 待测试
    async CANCEL_CHANGE({ state, commit, rootState, dispatch }: any, payload: any) {

        let resp = await cancelChange(payload).catch((err: any) => {
            Notification.error(err);
        })

        if (resp) {
            Message.success('取消成功!');
            dispatch('COMMIT_TABLE_DATA');
            return true;
        }
    },

    // 02_08_处理生产通知_提交/审核/批准
    async HANDLE_NOTICE({ state, commit, dispatch }: any, payload: any) {

        let resp = await commitNotice(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            dispatch('COMMIT_TABLE_DATA')
            return true
        }
    },

    // 02_09_通知单可见性
    async NOTICE_VISIBLE({ state, commit, rootState, dispatch }: any, payload: any) {

        let resp = await commitvisible(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            Message.success('设置成功!')
            dispatch('COMMIT_TABLE_DATA')
            return true
        }
        return false
    },


    //1-09审核
    async REVIEW_NOTICE({ state, commit, dispatch }: any, payload: any) {

        let resp = await approvalNotice(payload).catch((err: any) => {
            // 接口现在不能实现
            Notification.error(err)
        })

        if (resp) {
            Message.success('提交成功!')
            console.log(resp);
            //更新表格
            dispatch('COMMIT_TABLE_DATA')
            return true
        }
    },
    //1-09 提交通知单
    async COMMIT_NOTICE(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {

        let resp = await commitNotice(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            Message.success('提交成功!')
            console.log(resp);
            //更新表格
            dispatch('COMMIT_TABLE_DATA')
            return true
        }

    },
















    //1-06新建通知单产品
    async ADD_PRODUCT(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {

        let resp = await addProduct(payload).catch((err: any) => {
            Notification.error(err)
            return false
        })

        if (resp) {

            Message.success('添加成功!')
            console.log(resp);
            //更新表格
            //  dispatch('SET_ADD_NOTICE')
            return true



        }

    },

    //1-07 修改通知单产品
    async EDIT_PRODUCT(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {


        let resp = await editProduct(payload).catch((err: any) => {
            Notification.error(err)   //报错 说找不到error类型
            // console.log(err)
            // return false

        })

        if (resp) {
            Message.success('请求成功!')
            console.log(resp);
            return true
        }

    },

    //1-08删除通知单产品
    async DETELE_PRODCUT(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {


        let resp = await deleteProduct(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            Message.success('删除成功!')
            console.log(resp);
            return true
        }

    }


};



export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
};

/*

export default <any>basicModule(getNoticeList, {
    namespaced: true,
    state: {
       search:{},
    mutations: {


    },
    modules: {


    },
    actions: {
        //添加通知单
        async ADD_NOTICE({ state, commit, dispatch }: any, payload: any) {

            let resp = await addProductNotice(payload).catch((err: any) => {
               Notification.error(err.msg)
            })

            if (resp) {
                Message.success('新增成功!')
                //更新表格
              //  dispatch('SET_ADD_NOTICE')
                return true
            }
        },
        */
        //审核
/*
async REVIEW_NOTICE({ state, commit, dispatch }: any, payload: any) {

    let resp = await approvalNotice(payload).catch((err: any) => {
        // 接口现在不能实现
        //   Notification.error(err.msg)
    })

    if (resp) {
        Message.success('新增成功!')
        dispatch('COMMIT_TABLE_DATA')
        return true
    }
},
*/
        //删除通知单
/*
async DELETE_NOTICE({ state, commit, dispatch }: any, payload: any) {

    let resp = await deleteProductNotice(payload).catch((err: any) => {
        Notification.error(err.msg)
    })

    if (resp) {
        Message.success('删除成功!')
        dispatch('COMMIT_TABLE_DATA')
        return true
    }
},
}
},
})
*/
